<?php

/**
 * Centro form base class.
 *
 * @package    espazio
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfPropelFormGeneratedTemplate.php 12815 2008-11-09 10:43:58Z fabien $
 */
class BaseCentroForm extends BaseFormPropel
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                  => new sfWidgetFormInputHidden(),
      'nombre'              => new sfWidgetFormInput(),
      'url'                 => new sfWidgetFormInput(),
      'nota_corte'          => new sfWidgetFormInput(),
      'universidad_id'      => new sfWidgetFormPropelChoice(array('model' => 'Universidad', 'add_empty' => false)),
      'campus_id'           => new sfWidgetFormPropelChoice(array('model' => 'Campus', 'add_empty' => false)),
      'tipo_centro_id'      => new sfWidgetFormPropelChoice(array('model' => 'TipoCentro', 'add_empty' => false)),
      'centro_estudio_list' => new sfWidgetFormPropelChoiceMany(array('model' => 'Estudio')),
    ));

    $this->setValidators(array(
      'id'                  => new sfValidatorPropelChoice(array('model' => 'Centro', 'column' => 'id', 'required' => false)),
      'nombre'              => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'url'                 => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'nota_corte'          => new sfValidatorNumber(array('required' => false)),
      'universidad_id'      => new sfValidatorPropelChoice(array('model' => 'Universidad', 'column' => 'id')),
      'campus_id'           => new sfValidatorPropelChoice(array('model' => 'Campus', 'column' => 'id')),
      'tipo_centro_id'      => new sfValidatorPropelChoice(array('model' => 'TipoCentro', 'column' => 'id')),
      'centro_estudio_list' => new sfValidatorPropelChoiceMany(array('model' => 'Estudio', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('centro[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    parent::setup();
  }

  public function getModelName()
  {
    return 'Centro';
  }


  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['centro_estudio_list']))
    {
      $values = array();
      foreach ($this->object->getCentroEstudios() as $obj)
      {
        $values[] = $obj->getEstudioId();
      }

      $this->setDefault('centro_estudio_list', $values);
    }

  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->saveCentroEstudioList($con);
  }

  public function saveCentroEstudioList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['centro_estudio_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (is_null($con))
    {
      $con = $this->getConnection();
    }

    $c = new Criteria();
    $c->add(CentroEstudioPeer::CENTRO_ID, $this->object->getPrimaryKey());
    CentroEstudioPeer::doDelete($c, $con);

    $values = $this->getValue('centro_estudio_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new CentroEstudio();
        $obj->setCentroId($this->object->getPrimaryKey());
        $obj->setEstudioId($value);
        $obj->save();
      }
    }
  }

}
